Page({
  data: {
    houseLayerVisible: false, // 控制选择报修房屋面板的显示/隐藏
    houseList: [],

    repairLayerVisible: false, // 控制选择维修项目面板的显示/隐藏
    repairItem: [],

    dateLayerVisible: false, // 控制选择预约日期面板的显示/隐藏
    currentDate: new Date().getTime(),

    houseInfo: '', // 选择报修的房屋信息
    repairItemName: '', // 选择的维修项目

    houseId: '', // 已审核通过的房屋id
    repairItemId: '', // 维修项目id
    mobile: '',
    appointment: '', // 选择预约日期
    description: '', // 问题描述-最多200字
    attachment: [],
  },

  onLoad({ id }) {
    this.getHouseList()
    this.getRepairItem()
    if (id) {
      wx.setNavigationBarTitle({
        title: '修改报修信息',
      })
      this.getRepairDetail(id)
    }
  },

  async getRepairDetail(id) {
    if (!id) return
    // 请求数据接口
    const { code, data: repairDetail } = await wx.http.get('/repair/' + id)
    // 校验接口调用结果
    if (code !== 10000) return wx.utils.toast('获取报修详情失败!')
    this.setData({
      ...repairDetail,
    })
  },

  async getHouseList() {
    const { code, data: houseList } = await wx.http.get('/house')
    if (code !== 10000) return wx.utils.toast('获取房屋失败')
    this.setData({
      houseList,
    })
  },

  async getRepairItem() {
    const { code, data: repairItem } = await wx.http.get('/repairItem')
    if (code !== 10000) return wx.utils.toast('获取维修项目失败')
    this.setData({
      repairItem,
    })
  },

  // 1. 选择要保修的房屋
  selectHouse(e) {
    const { name: houseInfo, id: houseId } = e.detail
    this.setData({
      houseInfo,
      houseId,
    })
  },

  // 2. 选择要维修的项目
  selectRepairItem(e) {
    const { name: repairItemName, id: repairItemId } = e.detail
    this.setData({
      repairItemName,
      repairItemId,
    })
  },

  // 3. 选择预约日期
  selectAppointment(e) {
    this.setData({
      appointment: wx.utils.formatDate(e.detail),
      dateLayerVisible: false,
    })
  },

  // 4. 选择图片文件并上传
  async onAfterRead(e) {
    const res = await wx.http.upload('/upload', {
      name: 'file',
      filePath: e.detail.file.tempFilePath,
      formData: {},
    })
    if (res.code !== 10000) return wx.utils.toast('上传图片失败')
    // 2.2 更新数据
    const { attachment } = this.data
    attachment.push(res.data)
    this.setData({
      attachment,
    })
  },

  // 5. 删除图片
  onDelete(e) {
    // const { attachment } = this.data
    // attachment.splice(e.detail.index, 1)

    this.data.attachment.splice(e.detail.index, 1)
    this.setData({
      attachment: this.data.attachment,
    })
  },

  // 6. 提交报修
  async submit() {
    const { houseId, repairItemId, mobile, appointment, description, attachment, id } = this.data
    // 6.1 数据校验
    if (!wx.verify.verifyHouse(houseId)) return
    if (!wx.verify.verifyRepair(repairItemId)) return
    if (!wx.verify.verifyMobile(mobile)) return
    if (!wx.verify.verifyDate(appointment)) return
    if (!wx.verify.verifyDescription(description)) return
    // 6.2 调用接口
    const { code } = await wx.http.post('/repair', {
      id,
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment,
    })
    if (code !== 10000) return wx.utils.toast('提交报修失败~')
    // 6.3 跳转到报修列表页面
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index',
    })
  },

  // --------------打开/关闭选择报修房屋弹层-begin-----------------------
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  // --------------打开/关闭选择报修房屋弹层-end-----------------------

  // --------------打开/关闭选择维修项目弹层-begin-----------------------
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },
  // --------------打开/关闭选择维修项目弹层-end-----------------------

  // --------------打开/关闭选择预约日期弹层-begin-----------------------
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  // --------------打开/关闭选择预约日期弹层-end-----------------------
})
